/**
 * 
 */

/**
 * @author SERGIO MOLINA
 *
 */
public class DijkstraEngine {


public int[] execute(int[][] graph, int node) {

final boolean settledNodes[] = new boolean[graph.length];
final int shortestDistances[] = new int[graph.length];

settledNodes[0] = true;

for (int i = 1; i < shortestDistances.length; i++) {
if (graph[node][i] != 0) shortestDistances[i] = graph[node][i];
else shortestDistances[i] = Integer.MAX_VALUE;
}

for (int i = 0; i < (shortestDistances.length - 1); i++){
final int next = minVertex(shortestDistances, settledNodes);
settledNodes[next] = true;

for (int j = 0; j < graph[0].length; j++) {
final int d = shortestDistances[next] + graph[next][j];
if ( shortestDistances[j] > d) shortestDistances[j] = shortestDistances[next] + graph[next][j];
} 
}

return shortestDistances;
}


private static int minVertex (int [] shortestDistances, boolean [] settledNodes) {
int x = Integer.MAX_VALUE;
int y = -1;
for (int i = 0; i < shortestDistances.length; i++) {
if (!settledNodes[i] && shortestDistances[i] < x) {
y = i;
x = shortestDistances[i];
}
}
return y;
}
}